package myApp.KnorrAndroid.view.member;

import java.util.Vector;

import myApp.Constant.ActionEventConstant;
import myApp.Constant.ErrorConstants;
import myApp.Constant.IntentConstants;
import myApp.KnorrAndroid.R;
import myApp.KnorrAndroid.common.ActionEvent;
import myApp.KnorrAndroid.common.KnorrInfo;
import myApp.KnorrAndroid.common.ModelEvent;
import myApp.KnorrAndroid.controller.UserController;
import myApp.KnorrAndroid.view.KnorrBaseActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class LoginView extends KnorrBaseActivity implements OnClickListener{
	
	Button btLogin;
	TextView tvRegister;
	EditText etUserName;//username - email
	EditText etPassWord;//pass
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_login_view);
		btLogin = (Button)findViewById(R.id.btLogin);
		tvRegister = (TextView)findViewById(R.id.tvRegister);
		etUserName = (EditText)findViewById(R.id.etUserName);
		etPassWord = (EditText)findViewById(R.id.etPassWord);
		
		//set su kien onclick
		btLogin.setOnClickListener(this);
		tvRegister.setOnClickListener(this);
	}
	
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btLogin:
			requestLogin(etUserName.getEditableText().toString().trim(), 
					etPassWord.getEditableText().toString().trim());//tmp
			break;
		case R.id.tvRegister:
			gotoRegister();
			break;
		default:
			break;
		}
		
	}
	
	
	/**
	 *  qua man hinh dang ky thanh vien
	 *  @author: BangHN
	 *  @version: 1.0
	 *  @since: Jan 1, 2012
	 */
	private void gotoRegister(){
		Bundle bundle = new Bundle();
		ActionEvent e = new ActionEvent();
		e.sender = LoginView.this;
		e.viewData = bundle;
		e.action = ActionEventConstant.GO_TO_REGISTER_VIEW;
		UserController.getInstance().handleSwitchActivity(e);
	}
	
	/**
	 *  request login vao ung dung
	 *  @author: BangHN
	 *  @version: 1.0
	 *  @since: Jan 1, 2012
	 */
	private void requestLogin(String userName, String pass){
		Vector<String> data = new Vector<String>();
		data.addElement(IntentConstants.INTENT_USER_NAME);
		data.addElement(userName);
		data.addElement(IntentConstants.INTENT_PASSWORD);
		data.addElement(pass);
		data.addElement(IntentConstants.INTENT_PHONE_MODEL);
		data.addElement(KnorrInfo.getInstance().PHONE_MODEL);
		ActionEvent e = new ActionEvent();
		e.action = ActionEventConstant.LOGIN;
		e.viewData = data;
		e.sender = LoginView.this;
		UserController.getInstance().handleViewEvent(e);
	}
	
	
	@Override
	public void handleModelViewEvent(ModelEvent modelEvent) {
		// TODO Auto-generated method stub
		super.handleModelViewEvent(modelEvent);
	}
	
	@Override
	public void handleErrorModelViewEvent(ModelEvent modelEvent) {
		switch (modelEvent.getModelCode()) {
		case ErrorConstants.INVALID_EMAIL:
			showDialog("Email khong hop le");
			break;

		default:
			break;
		}
		super.handleErrorModelViewEvent(modelEvent);
	}
	
}
